package com.wyb.service.impl;

import com.wyb.dao.MemberDao;
import com.wyb.dao.impl.MemberDaoImpl;
import com.wyb.entity.Member;
import com.wyb.service.MemberService;
import com.wyb.util.JDBCUtils;

import java.sql.Connection;
import java.util.Collections;
import java.util.List;

public class MemberServiceImpl implements MemberService {
    MemberDao md=new MemberDaoImpl();
    @Override
    public List<Member> queryMemberAll() {
        return md.selectMemberAll();
    }

    @Override
    public boolean changeMemberState(int id, int state) {
        boolean boo=false;
        Connection conn=null;
        try {
            conn= JDBCUtils.startTransaction();
            if(md.updateMemberState(id,state)==1){
                boo=true;
            }
            JDBCUtils.commit(conn);
        } catch (Exception e) {
            JDBCUtils.rollback(conn);
            throw new RuntimeException(e);
        }
        return boo;
    }

    @Override
    public boolean addMember(Member member) {
        boolean boo=false;
        Connection conn=null;
        try {
            conn= JDBCUtils.startTransaction();
            if(md.insertMember(member)==1){
                boo=true;
            }
            JDBCUtils.commit(conn);
        } catch (Exception e) {
            JDBCUtils.rollback(conn);
            throw new RuntimeException(e);
        }
        return boo;
    }

    @Override
    public Member queryMemberById(int id) {
        return md.selectMemberById(id);
    }

    @Override
    public boolean changeMember(Member member) {
        boolean boo=false;
        Connection conn=null;
        try {
            conn= JDBCUtils.startTransaction();
            if(md.updateMember(member)==1){
                boo=true;
            }
            JDBCUtils.commit(conn);
        } catch (Exception e) {
            JDBCUtils.rollback(conn);
            throw new RuntimeException(e);
        }
        return boo;
    }

    @Override
    public boolean removeMember(int id) {
        boolean boo=false;
        Connection conn=null;
        try {
            conn= JDBCUtils.startTransaction();
            if(md.deleteMember(id)==1){
                boo=true;
            }
            JDBCUtils.commit(conn);
        } catch (Exception e) {
            JDBCUtils.rollback(conn);
            throw new RuntimeException(e);
        }
        return boo;
    }

    @Override
    public List<Member> queryMemberByRealName(String realName) {
        return md.selectMemberByRealName(realName);
    }
}
